Skip to main content

Using Terminal

I don't care if it works on your machine! We are not shipping your machine! - Vidiu Platon

About Terminal

Terminal, also known as command-line interface (CLI) or console, allows you to run commands on a computer without using a graphical user interface. CLI accepts text-based inputs which then converts into functions that the operating system understands. You can use various commands, from simple ones for switching directories to more complex such as running scripts with multiple flags.

How to open Terminal

Use Spotlight search on your Mac to find it.

  1. Press Cmd + Space on your keyboard to open Spotlight
  2. Type in Terminal and hit Enter

Now add it to the Dock to reach it more easily :)

  1. Right-click on the Terminal icon in the Dock
  2. Select Options
  3. Select Keep in Dock

Terminal settings

Terminal appearance

You might want to update the appearance of your Terminal to make it a bit more readable. There are quite a few options to change the color of the background, size and color of the text, cursor, and so on.

  1. Open Terminal
  2. Click Terminal in the toolbar
  3. Select Preferences
  4. Select the Profiles tab
  5. Select the Text tab

Change Terminal behavior

There are also various options to edit the Terminal behaviour, spread across multiple tabs: Window, Tab, Shell, Keyboard, Advanced.

For example, under the Shell tab you can specify the command that is run on startup:

  1. Select the Shell tab under Profiles
  2. Select the Run command checkbox
  3. In the input field, type in the command you wish to run, for example:
  • cd /Users to open the Terminal positioned to the Users directory
  • echo $$ to run the echo command right after the Terminal opens

Another interesting option is to close the Terminal window when the shell exits. This can be useful when you run shell commands from your project and don't want to leave the Terminal open.

  1. Select the Shell tab under Profiles
  2. From the dropdown in When the shell exits select Close the window option
    • if you run the exit command, the Terminal window will close

Terminal alternatives

The Terminal is not the only app for using the command line on the Mac. There are various alternatives such as iTerm2, Hyper, Upterm and Alacritty, to name a few. Each of these comes with some differences when compared to the Terminal, often adding additional features.

Shell

A shell is a computer program that basically allows you to control your computer. You can use it to control other programs, interact with the system, or simply automate repetitive tasks. The commands can be entered directly, or read from a file called the shell script.

To access a shell you need an app that runs it, such as Terminal/iTerm2 on Mac or Command Prompt/GitBash on Windows.

There are various shell programs to choose from, like sh, bash, ksh, tcsh and zsh.

  • sh or Bourne Shell

    • the original shell still used on UNIX systems
    • very basic with a few features
  • bash or Bourne Again Shell

    • the GNU Project's shell
    • it's compatible with sh, meaning commands that work in sh also work in bash (vice-versa is not always the case)
    • provides additional features and improvements over sh
  • zsh or Z-shell

    • extended version of sh, including some bash features
    • quite similar to bash but with some improvements and more customizable
    • commands that work in bash work in zsh

Command-line arguments

When reading about command-line arguments, you might come across terms like flag, switch, option, argument, parameter, and so on, which are often used interchangeably.

Arguments tell the process or function to perform a different operation depending on its value. Arguments are often used in command-line programs, and they are basically options that change the behaviour of a program.

There are a couple of types of arguments:

  • Arguments that take a value
  • Arguments that don't take a value
  • Positional arguments

Arguments can start with a single dash (-) or a double dash (--).

A flag argument usually refers to a boolean argument that by including it, it changes the behaviour of a program. If you include a flag argument its boolean value is set to true, if not, it is set to false. For example,

ls - lists all visible files and folders in the current folder, but if we add the flag -a, making it

ls -a - it lists all visible and invisible files and folders.

In the following example command, using the --report flag we tell the process to include/activate the report option. With the --report flag included a report is generated, and without the flag it is not.

python test.py --report

Arguments that start with a double dash (--) often take an argument themselves. For example, in a Python script we might have the env flag that activates a different environment depending on the value we specify when including the --env argument:

python test.py --env=test_environment

python test.py --env=staging_environment

For more details on arguments, read this blog post.

List of useful commands

The following list of commands works in the Terminal but might not work in other similar apps.

Directory / Folder navigation

CommandDescription
cd path/to/directoryPosition to a directory
cd ..Go back to the directory above the current one
cdGo back to the root (Home) directory

Permissions

There are three basic file system permissions (also known as modes) on Unix and Unix-like systems:

  • read (r)
  • write (w)
  • execute (x)

You can use the chmod (short for change mode) command to change the access permissions for a system file or folder.

CommandDescription
man chmodOpen chmod manual in the Terminal
chmod +x /path/to/fileAdd the executable permission for the file
chmod +rwx /path/to/fileAdd the permissions for the file
chmod -rwx /path/to/fileRemove the permissions for the file

Echo

CommandDescription
echo $$Get process ID (PID) of the current terminal/tab

Grep

grep is a command used for searching plain text.

It can be used on its own or in combination with other commands, separated by the vertical pipe symbol (|).

CommandDescription
grep "some text" file_name.txtSearches for "some text" in the specified file
ps -ax | grep AppiumDisplay processes that contain Appium

For more on the grep command, see this article.

List files and folders

CommandDescription
lsList all visible files and folders in the current folder
ls -aList all visible and invisible files and folders in the current folder
ls -lList all visible files and folders in the current folder with permissions

List of open files

CommandDescription
lsofList all open files and processes
lsof -i 6List open files that use IPv6 protocol
lsof -p 66460List open files and processes with PID 66460

Open file / directory

CommandDescription
open directory_nameOpen a directory
open directory_name/file_name.txtOpen a file
open ~/.zshrcOpen zsh resource file

Process status

CommandDescription
ps -axDisplays your and other users' processes
CommandDescription
pwdDisplay the path to the current/working directory

Close / Quit terminal

CommandDescription
exitQuit Terminal
python some_script.py; exitQuit Terminal right after running a command

System info

CommandDescription
id -unGet system name
hostnameGet hostname
system_profilerDisplay system information (long version)
system_profiler -detailLevel miniDisplay system information (short version)

Networking

CommandDescription
arp -aView a list of all active devices on a local network

Additional resources